/*
 * search_train_info_cb.c
 *
 *  Created on: 2013-2-5
 *      Author: panqiuhui
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "../head/gui_callback/search_train_info_cb.h"
#include "../head/gui_callback/global_cb.h"
#include "../head/common_struct/stationlist.h"
#include "../head/common_struct/start_end_stationlist.h"
#include "../head/common_struct/station_pass_trainlist.h"
#include "../head/communicate_with_server/traininfo_transfer.h"

extern GtkBuilder *builder;

enum {
	TRAIN_NUMBER_COL_ID = 0,
	TRAIN_NUMBER_COL_STATION,
	TRAIN_NUMBER_COL_ARRIVETIME,
	TRAIN_NUMBER_COL_DEPARTTIME,
	TRAIN_NUMBER_COL_TIMELAST,
	TRAIN_NUMBER_COL_PRICE
};

enum {
	START_END_STATION_COL_ID = 0,
	START_END_STATION_COL_TRAINID,
	START_END_STATION_COL_STARTSTATION,
	START_END_STATION_COL_ENDSTATION,
	START_END_STATION_COL_STARTTIME,
	START_END_STATION_COL_ENDTIME,
	START_END_STATION_COL_TIMELAST,
	START_END_STATION_COL_DEPARTSTATION,
	START_END_STATION_COL_ARRIVESTATION,
	START_END_STATION_COL_PRICE
};

enum {
	STATION_PASS_TRAIN_COL_ID = 0,
	STATION_PASS_TRAIN_COL_TRAINID,
	STATION_PASS_TRAIN_COL_DEPARTSTATION,
	STATION_PASS_TRAIN_COL_DEPARTTIME,
	STATION_PASS_TRAIN_COL_ARRIVESTATION,
	STATION_PASS_TRAIN_COL_ARRIVETIME,
	STATION_PASS_TRAIN_COL_STATIONNAME,
	STATION_PASS_TRAIN_COL_STARTTIME,
	STATION_PASS_TRAIN_COL_ENDTIME,
	STATION_PASS_TRAIN_COL_TIMELAST
};

//处理工具栏中的车次查询的点击事件
void menuitem_search_train_number_activate_cb(GtkMenuItem *menuitem,
		gpointer user_data) {
	GtkEntry *entry_search_train_number_inner_date;
	GtkCalendar *calendar_search_train_number_inner_date;
	GtkEntry *entry_search_train_number;
	GtkLabel *label_search_train_number_basicinfo_train;
	GtkLabel *label_search_train_number_basicinfo_aftertrain;
	GtkLabel *label_search_train_number_basicinfo_startstation;
	GtkLabel *label_search_train_number_basicinfo_afterstartstation;
	GtkLabel *label_search_train_number_basicinfo_endstation;
	GtkLabel *label_search_train_number_basicinfo_afterendstation;
	GtkLabel *label_search_train_number_basicinfo_stationnum;
	GtkLabel *label_search_train_number_basicinfo_afterstationnum;
	GtkLabel *label_search_train_number_basicinfo_before_search;
	GtkTreeView *treeview_search_train_number;

	entry_search_train_number_inner_date =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_search_train_number_inner_date"));
	calendar_search_train_number_inner_date =
			GTK_CALENDAR(gtk_builder_get_object(builder,"calendar_search_train_number_inner_date"));
	entry_search_train_number =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_search_train_number"));
	label_search_train_number_basicinfo_train =
			GTK_LABEL(gtk_builder_get_object(builder,"label_search_train_number_basicinfo_train"));
	label_search_train_number_basicinfo_aftertrain =
			GTK_LABEL(gtk_builder_get_object(builder,"label_search_train_number_basicinfo_aftertrain"));
	label_search_train_number_basicinfo_startstation =
			GTK_LABEL(gtk_builder_get_object(builder,"label_search_train_number_basicinfo_startstation"));
	label_search_train_number_basicinfo_afterstartstation =
			GTK_LABEL(gtk_builder_get_object(builder,"label_search_train_number_basicinfo_afterstartstation"));
	label_search_train_number_basicinfo_endstation =
			GTK_LABEL(gtk_builder_get_object(builder,"label_search_train_number_basicinfo_endstation"));
	label_search_train_number_basicinfo_afterendstation =
			GTK_LABEL(gtk_builder_get_object(builder,"label_search_train_number_basicinfo_afterendstation"));
	label_search_train_number_basicinfo_stationnum =
			GTK_LABEL(gtk_builder_get_object(builder,"label_search_train_number_basicinfo_stationnum"));
	label_search_train_number_basicinfo_afterstationnum =
			GTK_LABEL(gtk_builder_get_object(builder,"label_search_train_number_basicinfo_afterstationnum"));
	label_search_train_number_basicinfo_before_search =
			GTK_LABEL(gtk_builder_get_object(builder,"label_search_train_number_basicinfo_before_search"));
	treeview_search_train_number = GTK_TREE_VIEW(
			gtk_builder_get_object(builder, "treeview_search_train_number"));

	struct tm *tm_ptr;
	time_t the_time;
	(void) time(&the_time);
	tm_ptr = localtime(&the_time);
	char date[TRAIN_NUMBER_DATE_LEN];
	memset(date, 0, sizeof(date));
	sprintf(date, "%04d-%02d-%02d", tm_ptr->tm_year + 1900, tm_ptr->tm_mon + 1,
			tm_ptr->tm_mday);
	gtk_entry_set_text(entry_search_train_number_inner_date, date);
	gtk_calendar_select_month(calendar_search_train_number_inner_date,
			tm_ptr->tm_mon, tm_ptr->tm_year + 1900);
	gtk_calendar_select_day(calendar_search_train_number_inner_date,
			tm_ptr->tm_mday);

	gtk_entry_set_text(entry_search_train_number, "");

	gtk_label_set_label(label_search_train_number_basicinfo_train, "");
	gtk_label_set_label(label_search_train_number_basicinfo_startstation, "");
	gtk_label_set_label(label_search_train_number_basicinfo_endstation, "");
	gtk_label_set_label(label_search_train_number_basicinfo_stationnum, "");

	gtk_widget_hide(GTK_WIDGET(label_search_train_number_basicinfo_train) );
	gtk_widget_hide(
			GTK_WIDGET(label_search_train_number_basicinfo_aftertrain) );
	gtk_widget_hide(
			GTK_WIDGET(label_search_train_number_basicinfo_startstation) );
	gtk_widget_hide(
			GTK_WIDGET(label_search_train_number_basicinfo_afterstartstation) );
	gtk_widget_hide(
			GTK_WIDGET(label_search_train_number_basicinfo_endstation) );
	gtk_widget_hide(
			GTK_WIDGET(label_search_train_number_basicinfo_afterendstation) );
	gtk_widget_hide(
			GTK_WIDGET(label_search_train_number_basicinfo_stationnum) );
	gtk_widget_hide(
			GTK_WIDGET(label_search_train_number_basicinfo_afterstationnum) );
	gtk_widget_show(
			GTK_WIDGET(label_search_train_number_basicinfo_before_search) );

	GtkTreeModel *model;
	model = gtk_tree_view_get_model(treeview_search_train_number);
	gtk_list_store_clear(GTK_LIST_STORE(model) );

	window_show(SEARCH_TRAIN_INFO_TRAIN_NUMBER);
}

//处理车次查询按钮的点击事件
void button_search_train_number_clicked_cb(GtkButton *button,
		gpointer user_data) {
	GtkEntry *entry_search_train_number_inner_date;
	GtkEntry *entry_search_train_number;
	GtkLabel *label_search_train_number_basicinfo_train;
	GtkLabel *label_search_train_number_basicinfo_aftertrain;
	GtkLabel *label_search_train_number_basicinfo_startstation;
	GtkLabel *label_search_train_number_basicinfo_afterstartstation;
	GtkLabel *label_search_train_number_basicinfo_endstation;
	GtkLabel *label_search_train_number_basicinfo_afterendstation;
	GtkLabel *label_search_train_number_basicinfo_stationnum;
	GtkLabel *label_search_train_number_basicinfo_afterstationnum;
	GtkLabel *label_search_train_number_basicinfo_before_search;
	GtkTreeView *treeview_search_train_number;
	GtkWindow *global_window;

	global_window =
			GTK_WINDOW(gtk_builder_get_object(builder, "global_window"));
	entry_search_train_number_inner_date =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_search_train_number_inner_date"));
	entry_search_train_number =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_search_train_number"));
	label_search_train_number_basicinfo_train =
			GTK_LABEL(gtk_builder_get_object(builder,"label_search_train_number_basicinfo_train"));
	label_search_train_number_basicinfo_aftertrain =
			GTK_LABEL(gtk_builder_get_object(builder,"label_search_train_number_basicinfo_aftertrain"));
	label_search_train_number_basicinfo_startstation =
			GTK_LABEL(gtk_builder_get_object(builder,"label_search_train_number_basicinfo_startstation"));
	label_search_train_number_basicinfo_afterstartstation =
			GTK_LABEL(gtk_builder_get_object(builder,"label_search_train_number_basicinfo_afterstartstation"));
	label_search_train_number_basicinfo_endstation =
			GTK_LABEL(gtk_builder_get_object(builder,"label_search_train_number_basicinfo_endstation"));
	label_search_train_number_basicinfo_afterendstation =
			GTK_LABEL(gtk_builder_get_object(builder,"label_search_train_number_basicinfo_afterendstation"));
	label_search_train_number_basicinfo_stationnum =
			GTK_LABEL(gtk_builder_get_object(builder,"label_search_train_number_basicinfo_stationnum"));
	label_search_train_number_basicinfo_afterstationnum =
			GTK_LABEL(gtk_builder_get_object(builder,"label_search_train_number_basicinfo_afterstationnum"));
	label_search_train_number_basicinfo_before_search =
			GTK_LABEL(gtk_builder_get_object(builder,"label_search_train_number_basicinfo_before_search"));
	treeview_search_train_number = GTK_TREE_VIEW(
			gtk_builder_get_object(builder, "treeview_search_train_number"));

	const gchar *date = gtk_entry_get_text(
			entry_search_train_number_inner_date);
	const gchar *trainID = gtk_entry_get_text(entry_search_train_number);

	if (strlen(trainID) == 0) {
		gtk_show_warning(global_window, "%s", "车次不能为空", "输入提醒");
		return;
	}

	struct train_number_request request;
	memset(&request, 0, sizeof(request));
	strncpy(request.date, date, strlen(date));
	strncpy(request.trainID, trainID, strlen(trainID));

	GtkTreeModel *model;
	model = gtk_tree_view_get_model(treeview_search_train_number);
	gtk_list_store_clear(GTK_LIST_STORE(model) ); //清空liststore

	StationList stationlist; //声明车站列表
	InitializeListStation(&stationlist); //初始化车站列表
	bool search_result = search_train_number(&request, &stationlist);
	if (search_result) {
		StationNode *node = stationlist;
		GtkTreeIter iter;
		gint id = 1;
		bool isstart = true;

		while (node != NULL ) {
			StationItem item = node->item;
			if (isstart) {
				gtk_label_set_label(
						label_search_train_number_basicinfo_startstation,
						item.stationName);
				isstart = false;
			}
			if (node->next == NULL ) {
				gtk_label_set_label(
						label_search_train_number_basicinfo_endstation,
						item.stationName);
			}

			gtk_list_store_append(GTK_LIST_STORE(model), &iter);
			gtk_list_store_set(GTK_LIST_STORE(model), &iter,
					TRAIN_NUMBER_COL_ID, id, TRAIN_NUMBER_COL_STATION,
					item.stationName, TRAIN_NUMBER_COL_ARRIVETIME,
					item.stationArriveTime, TRAIN_NUMBER_COL_DEPARTTIME,
					item.stationDepartTime, TRAIN_NUMBER_COL_TIMELAST,
					item.consumeTime, TRAIN_NUMBER_COL_PRICE, item.price, -1);
			node = node->next;
			id++;
		}
		gtk_label_set_label(label_search_train_number_basicinfo_train, trainID);
		unsigned int station_count = ListItemCountStation(&stationlist);
		char count[10];
		memset(count, 0, sizeof(count));
		sprintf(count, "%d", station_count);
		gtk_label_set_label(label_search_train_number_basicinfo_stationnum,
				count);

		gtk_widget_show(GTK_WIDGET(label_search_train_number_basicinfo_train) );
		gtk_widget_show(
				GTK_WIDGET(label_search_train_number_basicinfo_aftertrain) );
		gtk_widget_show(
				GTK_WIDGET(label_search_train_number_basicinfo_startstation) );
		gtk_widget_show(
				GTK_WIDGET(label_search_train_number_basicinfo_afterstartstation) );
		gtk_widget_show(
				GTK_WIDGET(label_search_train_number_basicinfo_endstation) );
		gtk_widget_show(
				GTK_WIDGET(label_search_train_number_basicinfo_afterendstation) );
		gtk_widget_show(
				GTK_WIDGET(label_search_train_number_basicinfo_stationnum) );
		gtk_widget_show(
				GTK_WIDGET(label_search_train_number_basicinfo_afterstationnum) );
		gtk_widget_hide(
				GTK_WIDGET(label_search_train_number_basicinfo_before_search) );

	} else {
		gtk_show_info(global_window, "%s", "未查询到相应信息", "信息提示");
		return;
	}
	EmptyTheListStation(&stationlist); //释放车站列表空间
}

//处理工具栏中的发到站查询的点击事件
void menuitem_search_start_end_station_activate_cb(GtkMenuItem *menuitem,
		gpointer user_data) {
	GtkEntry *entry_search_start_end_station_date;
	GtkCalendar *calendar_search_start_end_station_date;
	GtkEntry *entry_search_start_end_station_start;
	GtkEntry *entry_search_start_end_station_end;
	GtkRadioButton *radiobutton_search_start_end_station_all;
	GtkRadioButton *radiobutton_search_start_end_station_begin;
	GtkRadioButton *radiobutton_search_start_end_station_end;
	GtkRadioButton *radiobutton_search_start_end_station_begin_end;
	GtkRadioButton *radiobutton_search_start_end_station_pass;
	GtkCheckButton *checkbutton_search_start_end_station_traintype_DG;
	GtkCheckButton *checkbutton_search_start_end_station_traintype_Z;
	GtkCheckButton *checkbutton_search_start_end_station_traintype_T;
	GtkCheckButton *checkbutton_search_start_end_station_traintype_K;
	GtkCheckButton *checkbutton_search_start_end_station_traintype_other;
	GtkTreeView *treeview_search_start_end_station;

	entry_search_start_end_station_date =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_search_start_end_station_date"));
	calendar_search_start_end_station_date =
			GTK_CALENDAR(gtk_builder_get_object(builder,"calendar_search_start_end_station_date"));
	entry_search_start_end_station_start =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_search_start_end_station_start"));
	entry_search_start_end_station_end =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_search_start_end_station_end"));
	radiobutton_search_start_end_station_all =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_search_start_end_station_all"));
	radiobutton_search_start_end_station_begin =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_search_start_end_station_begin"));
	radiobutton_search_start_end_station_end =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_search_start_end_station_end"));
	radiobutton_search_start_end_station_begin_end =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_search_start_end_station_begin_end"));
	radiobutton_search_start_end_station_pass =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_search_start_end_station_pass"));
	checkbutton_search_start_end_station_traintype_DG =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_search_start_end_station_traintype_DG"));
	checkbutton_search_start_end_station_traintype_Z =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_search_start_end_station_traintype_DG"));
	checkbutton_search_start_end_station_traintype_T =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_search_start_end_station_traintype_T"));
	checkbutton_search_start_end_station_traintype_K =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_search_start_end_station_traintype_K"));
	checkbutton_search_start_end_station_traintype_other =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_search_start_end_station_traintype_other"));
	treeview_search_start_end_station =
			GTK_TREE_VIEW(
					gtk_builder_get_object(builder, "treeview_search_start_end_station"));

	struct tm *tm_ptr;
	time_t the_time;
	(void) time(&the_time);
	tm_ptr = localtime(&the_time);
	char date[TRAIN_NUMBER_DATE_LEN];
	memset(date, 0, sizeof(date));
	sprintf(date, "%04d-%02d-%02d", tm_ptr->tm_year + 1900, tm_ptr->tm_mon + 1,
			tm_ptr->tm_mday);
	gtk_entry_set_text(entry_search_start_end_station_date, date);
	gtk_calendar_select_month(calendar_search_start_end_station_date,
			tm_ptr->tm_mon, tm_ptr->tm_year + 1900);
	gtk_calendar_select_day(calendar_search_start_end_station_date,
			tm_ptr->tm_mday);

	gtk_entry_set_text(entry_search_start_end_station_start, "");
	gtk_entry_set_text(entry_search_start_end_station_end, "");

	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
			radiobutton_search_start_end_station_all), true);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
			radiobutton_search_start_end_station_begin), false);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
			radiobutton_search_start_end_station_end), false);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
			radiobutton_search_start_end_station_begin_end), false);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
			radiobutton_search_start_end_station_pass), false);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
			checkbutton_search_start_end_station_traintype_DG), true);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
			checkbutton_search_start_end_station_traintype_Z), true);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
			checkbutton_search_start_end_station_traintype_T), true);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
			checkbutton_search_start_end_station_traintype_K), true);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
			checkbutton_search_start_end_station_traintype_other), true);

	GtkTreeModel *model;
	model = gtk_tree_view_get_model(treeview_search_start_end_station);
	gtk_list_store_clear(GTK_LIST_STORE(model) );

	window_show(SEARCH_TRAIN_INFO_START_END_STATION);
}

//处理发到站查询按钮的点击事件
void button_search_start_end_station_clicked_cb(GtkButton *button,
		gpointer user_data) {
	GtkEntry *entry_search_start_end_station_date;
	GtkEntry *entry_search_start_end_station_start;
	GtkEntry *entry_search_start_end_station_end;
	GtkRadioButton *radiobutton_search_start_end_station_all;
	GtkRadioButton *radiobutton_search_start_end_station_begin;
	GtkRadioButton *radiobutton_search_start_end_station_end;
	GtkRadioButton *radiobutton_search_start_end_station_begin_end;
	GtkRadioButton *radiobutton_search_start_end_station_pass;
	GtkCheckButton *checkbutton_search_start_end_station_traintype_DG;
	GtkCheckButton *checkbutton_search_start_end_station_traintype_Z;
	GtkCheckButton *checkbutton_search_start_end_station_traintype_T;
	GtkCheckButton *checkbutton_search_start_end_station_traintype_K;
	GtkCheckButton *checkbutton_search_start_end_station_traintype_other;
	GtkTreeView *treeview_search_start_end_station;
	GtkWindow *global_window;

	global_window =
			GTK_WINDOW(gtk_builder_get_object(builder, "global_window"));
	entry_search_start_end_station_date =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_search_start_end_station_date"));
	entry_search_start_end_station_start =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_search_start_end_station_start"));
	entry_search_start_end_station_end =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_search_start_end_station_end"));
	radiobutton_search_start_end_station_all =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_search_start_end_station_all"));
	radiobutton_search_start_end_station_begin =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_search_start_end_station_begin"));
	radiobutton_search_start_end_station_end =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_search_start_end_station_end"));
	radiobutton_search_start_end_station_begin_end =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_search_start_end_station_begin_end"));
	radiobutton_search_start_end_station_pass =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_search_start_end_station_pass"));
	checkbutton_search_start_end_station_traintype_DG =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_search_start_end_station_traintype_DG"));
	checkbutton_search_start_end_station_traintype_Z =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_search_start_end_station_traintype_Z"));
	checkbutton_search_start_end_station_traintype_T =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_search_start_end_station_traintype_T"));
	checkbutton_search_start_end_station_traintype_K =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_search_start_end_station_traintype_K"));
	checkbutton_search_start_end_station_traintype_other =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_search_start_end_station_traintype_other"));
	treeview_search_start_end_station =
			GTK_TREE_VIEW(
					gtk_builder_get_object(builder, "treeview_search_start_end_station"));

	GtkTreeModel *model;
	model = gtk_tree_view_get_model(treeview_search_start_end_station);
	gtk_list_store_clear(GTK_LIST_STORE(model) );

	const gchar *date = gtk_entry_get_text(entry_search_start_end_station_date);
	const gchar *start_station = gtk_entry_get_text(
			entry_search_start_end_station_start);
	const gchar *end_station = gtk_entry_get_text(
			entry_search_start_end_station_end);

	if (strlen(start_station) == 0) {
		gtk_show_warning(global_window, "%s", "发站不能为空", "输入提醒");
		return;
	}

	if (strlen(end_station) == 0) {
		gtk_show_warning(global_window, "%s", "到站不能为空", "输入提醒");
		return;
	}

	int passStationType = TYPE_PASS_STATION_ALL;
	gboolean isPassStationTypeALL = gtk_toggle_button_get_active(
			GTK_TOGGLE_BUTTON(
					radiobutton_search_start_end_station_all) );
	if (isPassStationTypeALL) {
		passStationType = TYPE_PASS_STATION_ALL;
	}
	gboolean isPassStationTypeBegin = gtk_toggle_button_get_active(
			GTK_TOGGLE_BUTTON(
					radiobutton_search_start_end_station_begin) );
	if (isPassStationTypeBegin) {
		passStationType = TYPE_PASS_STATION_BEGIN;
	}
	gboolean isPassStationTypeEnd = gtk_toggle_button_get_active(
			GTK_TOGGLE_BUTTON(
					radiobutton_search_start_end_station_end) );
	if (isPassStationTypeEnd) {
		passStationType = TYPE_PASS_STATION_END;
	}
	gboolean isPassStationTypeBeginEnd = gtk_toggle_button_get_active(
			GTK_TOGGLE_BUTTON(
					radiobutton_search_start_end_station_begin_end) );
	if (isPassStationTypeBeginEnd) {
		passStationType = TYPE_PASS_STATION_BEGIN_END;
	}
	gboolean isPassStationTypePass = gtk_toggle_button_get_active(
			GTK_TOGGLE_BUTTON(
					radiobutton_search_start_end_station_pass) );
	if (isPassStationTypePass) {
		passStationType = TYPE_PASS_STATION_PASS;
	}

	int trainType = TYPE_TRAIN_TYPE_NONE;
	gboolean isTrainTypeDG = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
			checkbutton_search_start_end_station_traintype_DG) );
	if (isTrainTypeDG) {
		trainType += TYPE_TRAIN_TYPE_DG;
	}
	gboolean isTrainTypeZ = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
			checkbutton_search_start_end_station_traintype_Z) );
	if (isTrainTypeZ) {
		trainType += TYPE_TRAIN_TYPE_Z;
	}
	gboolean isTrainTypeT = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
			checkbutton_search_start_end_station_traintype_T) );
	if (isTrainTypeT) {
		trainType += TYPE_TRAIN_TYPE_T;
	}
	gboolean isTrainTypeK = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
			checkbutton_search_start_end_station_traintype_K) );
	if (isTrainTypeK) {
		trainType += TYPE_TRAIN_TYPE_K;
	}
	gboolean isTrainTypeOther = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
			checkbutton_search_start_end_station_traintype_other) );
	if (isTrainTypeOther) {
		trainType += TYPE_TRAIN_TYPE_OTHER;
	}

	if (trainType == TYPE_TRAIN_TYPE_NONE) {
		gtk_show_warning(global_window, "%s", "请选择列车类型", "输入提醒");
		return;
	}

	struct train_start_end_station_request request;
	memset(&request, 0, sizeof(request));
	strncpy(request.date, date, strlen(date));
	g_utf8_strncpy(request.startStation, start_station,
			g_utf8_strlen(start_station, STATIONS_STATION_NAME_LEN));
	g_utf8_strncpy(request.endStation, end_station,
			g_utf8_strlen(end_station, STATIONS_STATION_NAME_LEN));
	request.passStationType = passStationType;
	request.trainType = trainType;

	StartEndStationList start_end_stationlist; //声明列表
	InitializeListStartEndStation(&start_end_stationlist); //初始化列表
	bool search_result = search_start_end_station(&request,
			&start_end_stationlist);
	if (search_result) {
		StartEndStationNode *node = start_end_stationlist;
		GtkTreeIter iter;
		gint id = 1;

		while (node != NULL ) {
			StartEndStationItem item = node->item;
			gtk_list_store_append(GTK_LIST_STORE(model), &iter);
			gtk_list_store_set(GTK_LIST_STORE(model), &iter,
					START_END_STATION_COL_ID, id, START_END_STATION_COL_TRAINID,
					item.trainID, START_END_STATION_COL_STARTSTATION,
					item.startStation, START_END_STATION_COL_ENDSTATION,
					item.endStation, START_END_STATION_COL_STARTTIME,
					item.startTime, START_END_STATION_COL_ENDTIME, item.endTime,
					START_END_STATION_COL_TIMELAST, item.consumeTime,
					START_END_STATION_COL_DEPARTSTATION, item.departStation,
					START_END_STATION_COL_ARRIVESTATION, item.arriveStation,
					START_END_STATION_COL_PRICE, item.price, -1);
			node = node->next;
			id++;
		}
	} else {
		gtk_show_info(global_window, "%s", "未找到相应信息", "信息提示");
	}
	EmptyTheListStartEndStation(&start_end_stationlist);
}

//处理工具栏中的车站经过车次查询的点击事件
void menuitem_search_station_pass_train_activate_cb(GtkMenuItem *menuitem,
		gpointer user_data) {
	GtkEntry *entry_search_station_pass_train_station;
	GtkRadioButton *radiobutton_search_station_pass_train_all;
	GtkRadioButton *radiobutton_search_station_pass_train_begin;
	GtkRadioButton *radiobutton_search_station_pass_train_end;
	GtkRadioButton *radiobutton_search_station_pass_train_pass;
	GtkCheckButton *checkbutton_search_station_pass_train_traintype_DG;
	GtkCheckButton *checkbutton_search_station_pass_train_traintype_Z;
	GtkCheckButton *checkbutton_search_station_pass_train_traintype_T;
	GtkCheckButton *checkbutton_search_station_pass_train_traintype_K;
	GtkCheckButton *checkbutton_search_station_pass_train_traintype_other;
	GtkTreeView *treeview_search_station_pass_train;

	entry_search_station_pass_train_station =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_search_station_pass_train_station"));
	radiobutton_search_station_pass_train_all =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_search_start_end_station_all"));
	radiobutton_search_station_pass_train_begin =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_search_station_pass_train_begin"));
	radiobutton_search_station_pass_train_end =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_search_station_pass_train_end"));
	radiobutton_search_station_pass_train_pass =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_search_station_pass_train_pass"));
	checkbutton_search_station_pass_train_traintype_DG =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_search_station_pass_train_traintype_DG"));
	checkbutton_search_station_pass_train_traintype_Z =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_search_station_pass_train_traintype_Z"));
	checkbutton_search_station_pass_train_traintype_T =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_search_station_pass_train_traintype_T"));
	checkbutton_search_station_pass_train_traintype_K =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_search_station_pass_train_traintype_K"));
	checkbutton_search_station_pass_train_traintype_other =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_search_station_pass_train_traintype_other"));
	treeview_search_station_pass_train =
			GTK_TREE_VIEW(
					gtk_builder_get_object(builder, "treeview_search_station_pass_train"));

	gtk_entry_set_text(entry_search_station_pass_train_station, "");
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
			radiobutton_search_station_pass_train_all), true);
	gtk_toggle_button_set_active(
			GTK_TOGGLE_BUTTON(radiobutton_search_station_pass_train_begin),
			false);
	gtk_toggle_button_set_active(
			GTK_TOGGLE_BUTTON(radiobutton_search_station_pass_train_end),
			false);
	gtk_toggle_button_set_active(
			GTK_TOGGLE_BUTTON(radiobutton_search_station_pass_train_pass),
			false);
	gtk_toggle_button_set_active(
			GTK_TOGGLE_BUTTON(checkbutton_search_station_pass_train_traintype_DG),
			true);
	gtk_toggle_button_set_active(
			GTK_TOGGLE_BUTTON(checkbutton_search_station_pass_train_traintype_Z),
			true);
	gtk_toggle_button_set_active(
			GTK_TOGGLE_BUTTON(checkbutton_search_station_pass_train_traintype_T),
			true);
	gtk_toggle_button_set_active(
			GTK_TOGGLE_BUTTON(checkbutton_search_station_pass_train_traintype_K),
			true);
	gtk_toggle_button_set_active(
			GTK_TOGGLE_BUTTON(checkbutton_search_station_pass_train_traintype_other),
			true);

	GtkTreeModel *model;
	model = gtk_tree_view_get_model(treeview_search_station_pass_train);
	gtk_list_store_clear(GTK_LIST_STORE(model) );

	window_show(SEARCH_TRAIN_INFO_STATION_PASS_TRAIN);
}

//处理车站经过车次查询按钮的点击事件
void button_search_station_pass_train_clicked_cb(GtkButton *button,
		gpointer user_data) {
	GtkEntry *entry_search_station_pass_train_station;
	GtkRadioButton *radiobutton_search_station_pass_train_all;
	GtkRadioButton *radiobutton_search_station_pass_train_begin;
	GtkRadioButton *radiobutton_search_station_pass_train_end;
	GtkRadioButton *radiobutton_search_station_pass_train_pass;
	GtkCheckButton *checkbutton_search_station_pass_train_traintype_DG;
	GtkCheckButton *checkbutton_search_station_pass_train_traintype_Z;
	GtkCheckButton *checkbutton_search_station_pass_train_traintype_T;
	GtkCheckButton *checkbutton_search_station_pass_train_traintype_K;
	GtkCheckButton *checkbutton_search_station_pass_train_traintype_other;
	GtkTreeView *treeview_search_station_pass_train;
	GtkWindow *global_window;

	global_window =
			GTK_WINDOW(gtk_builder_get_object(builder, "global_window"));
	entry_search_station_pass_train_station =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_search_station_pass_train_station"));
	radiobutton_search_station_pass_train_all =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_search_start_end_station_all"));
	radiobutton_search_station_pass_train_begin =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_search_station_pass_train_begin"));
	radiobutton_search_station_pass_train_end =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_search_station_pass_train_end"));
	radiobutton_search_station_pass_train_pass =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_search_station_pass_train_pass"));
	checkbutton_search_station_pass_train_traintype_DG =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_search_station_pass_train_traintype_DG"));
	checkbutton_search_station_pass_train_traintype_Z =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_search_station_pass_train_traintype_Z"));
	checkbutton_search_station_pass_train_traintype_T =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_search_station_pass_train_traintype_T"));
	checkbutton_search_station_pass_train_traintype_K =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_search_station_pass_train_traintype_K"));
	checkbutton_search_station_pass_train_traintype_other =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_search_station_pass_train_traintype_other"));
	treeview_search_station_pass_train =
			GTK_TREE_VIEW(
					gtk_builder_get_object(builder, "treeview_search_station_pass_train"));

	GtkTreeModel *model;
	model = gtk_tree_view_get_model(treeview_search_station_pass_train);
	gtk_list_store_clear(GTK_LIST_STORE(model) );

	const gchar *station = gtk_entry_get_text(
			entry_search_station_pass_train_station);

	if (g_utf8_strlen(station, STATIONS_STATION_NAME_LEN) == 0) {
		gtk_show_warning(global_window, "%s", "请输入车站", "输入提醒");
		return;
	}

	int passStationType = TYPE_PASS_STATION_ALL;
	gboolean isPassStationTypeALL = gtk_toggle_button_get_active(
			GTK_TOGGLE_BUTTON(
					radiobutton_search_station_pass_train_all) );
	if (isPassStationTypeALL) {
		passStationType = TYPE_PASS_STATION_ALL;
	}
	gboolean isPassStationTypeBegin = gtk_toggle_button_get_active(
			GTK_TOGGLE_BUTTON(
					radiobutton_search_station_pass_train_begin) );
	if (isPassStationTypeBegin) {
		passStationType = TYPE_PASS_STATION_BEGIN;
	}
	gboolean isPassStationTypeEnd = gtk_toggle_button_get_active(
			GTK_TOGGLE_BUTTON(
					radiobutton_search_station_pass_train_end) );
	if (isPassStationTypeEnd) {
		passStationType = TYPE_PASS_STATION_END;
	}
	gboolean isPassStationTypePass = gtk_toggle_button_get_active(
			GTK_TOGGLE_BUTTON(
					radiobutton_search_station_pass_train_pass) );
	if (isPassStationTypePass) {
		passStationType = TYPE_PASS_STATION_PASS;
	}

	int trainType = TYPE_TRAIN_TYPE_NONE;
	gboolean isTrainTypeDG = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
			checkbutton_search_station_pass_train_traintype_DG) );
	if (isTrainTypeDG) {
		trainType += TYPE_TRAIN_TYPE_DG;
	}
	gboolean isTrainTypeZ = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
			checkbutton_search_station_pass_train_traintype_Z) );
	if (isTrainTypeZ) {
		trainType += TYPE_TRAIN_TYPE_Z;
	}
	gboolean isTrainTypeT = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
			checkbutton_search_station_pass_train_traintype_T) );
	if (isTrainTypeT) {
		trainType += TYPE_TRAIN_TYPE_T;
	}
	gboolean isTrainTypeK = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
			checkbutton_search_station_pass_train_traintype_K) );
	if (isTrainTypeK) {
		trainType += TYPE_TRAIN_TYPE_K;
	}
	gboolean isTrainTypeOther = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
			checkbutton_search_station_pass_train_traintype_other) );
	if (isTrainTypeOther) {
		trainType += TYPE_TRAIN_TYPE_OTHER;
	}

	if (trainType == TYPE_TRAIN_TYPE_NONE) {
		gtk_show_warning(global_window, "%s", "请选择列车类型", "输入提醒");
		return;
	}

	struct station_pass_train_request request;
	memset(&request, 0, sizeof(request));
	g_utf8_strncpy(request.stationName, station,
			g_utf8_strlen(station, STATIONS_STATION_NAME_LEN));
	request.passStationType = passStationType;
	request.trainType = trainType;
	StationPassTrainList station_pass_train_list; //声明列表
	InitializeListStationPassTrain(&station_pass_train_list); //初始化列表
	bool search_result = search_station_pass_train(&request,
			&station_pass_train_list);
	if (search_result) {
		StationPassTrainNode *node = station_pass_train_list;
		GtkTreeIter iter;
		gint id = 1;

		while (node != NULL ) {
			StationPassTrainItem item = node->item;
			gtk_list_store_append(GTK_LIST_STORE(model), &iter);
			gtk_list_store_set(GTK_LIST_STORE(model), &iter,
					STATION_PASS_TRAIN_COL_ID, id,
					STATION_PASS_TRAIN_COL_TRAINID, item.trainID,
					STATION_PASS_TRAIN_COL_DEPARTSTATION, item.departStation,
					STATION_PASS_TRAIN_COL_DEPARTTIME, item.departTime,
					STATION_PASS_TRAIN_COL_ARRIVESTATION, item.arriveStation,
					STATION_PASS_TRAIN_COL_ARRIVETIME, item.arriveTime,
					STATION_PASS_TRAIN_COL_STATIONNAME, item.stationName,
					STATION_PASS_TRAIN_COL_STARTTIME, item.startTime,
					STATION_PASS_TRAIN_COL_ENDTIME, item.endTime,
					STATION_PASS_TRAIN_COL_TIMELAST, item.consumeTime, -1);
			node = node->next;
			id++;
		}

	} else {
		gtk_show_info(global_window, "%s", "未找到相应信息", "信息提示");
	}
	EmptyTheListStationPassTrain(&station_pass_train_list);
}

